#include <linux/module.h>
#include <linux/fs.h>
#include <linux/platform_device.h>
#include <linux/of.h>

int major;				//设备号
static struct class *my_dev_class;
int dev_cnt;
int dev_pins[10];

/*=============================file_operations ==============================*/
static ssize_t my_drv_read (struct file *filp, char __user *buf, size_t size, loff_t *offset)
{
	printk("drv_read function run....\n");
	return 1;
}

static ssize_t my_drv_write (struct file *filp, const char __user *buf, size_t size, loff_t *offset)
{
	printk("drv_write function run....\n");
	return 0;
}

static int my_drv_open (struct inode *node, struct file *filp)
{
	printk("drv_open function run....\n");
	return 0;
}

static int my_drv_release (struct inode *node, struct file *filp)
{

	printk("drv_release function run....\n");
	return 0;
}

/* operations结构体：为应用层提供驱动接口 */
static struct file_operations my_dev_ops = {
	.owner		= 	THIS_MODULE,
	.read 		=	my_drv_read,
	.write		=	my_drv_write,
	.open		=	my_drv_open,
	.release	=	my_drv_release,
};

/*=============================platform_driver==============================*/
/*  如果匹配到了内核根据设备树生成的platform_device，
	该函数会被调用，如果有多个匹配的设备节点，该函数
	会被多次调用
*/
static int my_probe(struct platform_device *pdev)
{
	/*  从内核根据设备树生成的 platform_device 
		结构体中获取到设备节点
	*/
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	
	int pin;
	char a[20];
	const char *str = a;
	
	of_property_read_u32(np, "pin", &pin);
	of_property_read_string(np, "my_name", &str);

	//保存设备的引脚
	dev_pins[dev_cnt] = pin;  
	//创建设备节点 /dev/xxx
	device_create(my_dev_class, NULL, MKDEV(major, dev_cnt), NULL, str);
	dev_cnt++;

	printk("my_probe run, my_name = %s\n", str);
	
	return 0;
}

static int my_remove(struct platform_device *pdev)
{
	/*  从内核根据设备树生成的 platform_device 
		结构体中获取到设备节点
	*/
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	
	int pin, i;
	char a[20];
	const char *str = a;
	
	of_property_read_u32(np, "pin", &pin);
	of_property_read_string(np, "my_name", &str);

	for(i = 0; i < dev_cnt; i++){
		if(dev_pins[i] == pin){
			dev_pins[i] = -1;
			device_destroy(my_dev_class, MKDEV(major, i));
			break;
		}
	}
	
	printk("my_remove run, device_destroy %s\n", str);
	return 0;
}

static struct of_device_id my_dev_match[] = {
	{.compatible = "hc-led-beep"}, 
	{.compatible = "hc-led-beep"}, 
	{.compatible = "hc-key"}, 
	{},
};

static struct platform_driver dev_driver = {
	.probe		=	my_probe,	
	.remove		= 	my_remove,
	.driver		= {
		.name	= "my_platform_driver",
		.of_match_table = my_dev_match,
	},
};

/*=============================驱动出入口函数==============================*/
/* 驱动入口函数：insmod xx.ko 时会被调用 */
static int dev_init(void)
{	
	major = register_chrdev(0, "hc_dev_drv", &my_dev_ops);
	if(major < 0){
		printk("register_chrdev famy\n");
		return major;
	}

	my_dev_class = class_create
(THIS_MODULE, "my_dev_class");
	if(IS_ERR(my_dev_class)){
		printk("class_create failed\n");
		return 1;
	}

	platform_driver_register(&dev_driver);

	return 0;
}

/* 驱动出口函数： rmmod xx.ko 时会被调用 */
static void dev_exit(void)
{
	platform_driver_unregister(&dev_driver);
	class_destroy(my_dev_class);
	unregister_chrdev(major, "hc_dev_drv");
	printk("my_dev driver exit\n");
}

module_init(dev_init);
module_exit(dev_exit);
MODULE_LICENSE("GPL");
